package com.admin.kotlintest.activity.setting

import android.content.Intent
import android.os.Bundle
import android.view.View
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.admin.kotlintest.R
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.adapter.SubFunctionAdapter
import com.admin.kotlintest.bean.SubFunction
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_sysytem_setting.*
import kotlinx.android.synthetic.main.layout_title.*
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/4/11.
 * 设置列表
 */
class SystemSettingActivity : BaseActivity(){

    var subAdapter: SubFunctionAdapter?=null
    var objectId:String ?=null

    override fun bindLayout(): Int {
        return R.layout.activity_sysytem_setting
    }

    override fun initData() {
        tv_title.text="系统设置"
        objectId=intent.getStringExtra("objectId")
        subAdapter= SubFunctionAdapter(context,R.layout.layout_sub_function_item,null)
        lv_system_function.adapter=subAdapter

        var query= BmobQuery<SubFunction>()
        query.addWhereEqualTo("parentId",objectId)
        query.addWhereEqualTo("disabled",false)
        query.setLimit(100)
        query.findObjects(object : FindListener<SubFunction>(){
            override fun done(p0: MutableList<SubFunction>?, p1: BmobException?) {
                if(p1==null && p0!=null){
                    subAdapter!!.upData(p0)
                }
            }
        })

    }

    override fun initEvent() {
        lv_system_function.setOnItemClickListener { _, _, position, _ ->
            var function=subAdapter!!.getItem(position)
            if(function.action!=null&& function.action != "") {
                var action = Intent(function.action)
                KLog.d(function.objectId)
                action.putExtra("objectId",function.objectId)
                startActivity(action)
            }else{
                toast("功能开发中，请等待")
            }
        }

        iv_title_left.visibility= View.VISIBLE
        iv_title_left.setOnClickListener{
            this.finish()
        }
    }
}